package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.dao.CourierStationAddDao;
import com.hualan.dao.impl.CourierStationAddDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/courierStation/addCourierStation")
public class CourierStationAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<>();

        // 接收数据
        String courierStationAddress = req.getParameter("courierStationAddress");
        String courierStationCity = req.getParameter("courierStationCity");
        String courierStationCommunityId = req.getParameter("courierStationCommunityId");
        String courierStationDistrict = req.getParameter("courierStationDistrict");
        String courierStationImage = req.getParameter("courierStationImage");
        String courierStationName = req.getParameter("courierStationName");
        String courierStationProvince = req.getParameter("courierStationProvince");

        // 判断小区id为空
        if (courierStationCommunityId == null || "".equals(courierStationCommunityId)){
            courierStationCommunityId = "1";
        }

        System.out.println("后端收到的数据" + courierStationCity);
        // 创建courierStationAddDao对象
        CourierStationAddDao courierStationAddDao = new CourierStationAddDaoImpl();
        int row = courierStationAddDao.addCourierStation(courierStationAddress, courierStationCity, courierStationCommunityId, courierStationDistrict, courierStationImage, courierStationName, courierStationProvince);
        if (row < 1){
            map.put("code", 302);
            map.put("message", "新增失败");
            return;
        }else {
            map.put("code", 200);
            map.put("message", "新增成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
